package com.card.server;

import java.util.List;

import javax.jdo.PersistenceManager;

import com.card.client.entry.FinanceType;
import com.card.client.service.FinanceTypeConfigService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("unchecked")
public class FinanceTypeConfigImpl extends RemoteServiceServlet implements
		FinanceTypeConfigService {

	private static final long serialVersionUID = -5977011520612518703L;

	@Override
	public void addFinanceType(FinanceType financeType) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		if(financeType != null){
			pm.makePersistent(financeType);
		}
		pm.close();
	}

	@Override
	public void delFinanceType(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		if(id != null){
			List<FinanceType> result = (List<FinanceType>)pm.newQuery(FinanceType.class).execute();
			if(result.size()>0){
				for(int i=0;i<result.size();i++){
					FinanceType ft = result.get(0);
					if(ft.getId().equals(id)){
						pm.deletePersistent(ft);
						break;
					}
				}
			}
			
		}
		pm.close();
	}

	@Override
	public FinanceType[] getAllFinanceType() {
		FinanceType[] result = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<FinanceType> list = (List<FinanceType>)pm.newQuery(FinanceType.class).execute();
		if(list != null){
			result = new FinanceType[list.size()];
			for(int i=0;i<result.length;i++){
				result[i] = list.get(i);
			}
		} else {
			result = new FinanceType[0];
		}
		pm.close();
		return result;
	}

	@Override
	public void updateFinanceType(FinanceType financeType) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		if(financeType != null){
			pm.makePersistent(financeType);
		}
		pm.close();
	}

}
